﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace GKFX.Common.UI.CookieManager
{
    public static class CookieWriter
    {
        #region Domain Operations
        public static void SetCookieForAllDomains(string CookieName, string CookieValue)
        {
            if (HttpContext.Current.Request.Url.Host == "localhost")
            {
                try
                {
                    var oldCookie = new HttpCookie(CookieName) { Expires = DateTime.Today.AddYears(-1) };
                    //oldCookie.Domain = domain;
                    HttpContext.Current.Response.Cookies.Add(oldCookie);


                    var newCookie = new HttpCookie(CookieName) { Expires = DateTime.Today.AddYears(1) };
                    HttpContext.Current.Response.Cookies.Add(newCookie);

                    newCookie.Value = CookieValue;
                    // newCookie.Domain = domain;
                }
                catch { }

            }
            else
            {
                foreach (string domain in ConfManager.Instance.CookieManager.CookieDomains.Value)
                {
                    //if (!HttpContext.Current.Request.Url.DnsSafeHost.Contains(domain))
                    //{
                    //    continue;
                    //}
                    try
                    {
                        var oldCookie = new HttpCookie(CookieName) { Expires = DateTime.Today.AddYears(-1) };
                        oldCookie.Domain = domain;
                        HttpContext.Current.Response.Cookies.Add(oldCookie);

                        var newCookie = new HttpCookie(CookieName) { Expires = DateTime.Today.AddYears(1) };
                        HttpContext.Current.Response.Cookies.Add(newCookie);

                        newCookie.Value = CookieValue;
                        newCookie.Domain = domain;
                    }
                    catch { }

                }
            }

        }

        #endregion Domain Operations
    }
}
